package com.minisoft.workhour.util;

import com.minisoft.workhour.model.ManhourRequest;
import com.minisoft.workhour.service.JsonFileService;
import com.minisoft.workhour.service.ManhourApiService;

import java.util.List;

/**
 * JSON工时数据提交工具
 */
public class JsonManhourSubmitter {

    private JsonFileService jsonFileService;
    private ManhourApiService manhourApiService;

    public JsonManhourSubmitter() {
        this.jsonFileService = new JsonFileService();
        this.manhourApiService = new ManhourApiService();
    }

    /**
     * 从JSON文件读取并提交工时数据
     *
     * @param filePath JSON文件路径
     * @return 提交结果统计
     */
    public SubmitResult submitFromJsonFile(String filePath) {
        if (filePath == null || filePath.trim().isEmpty()) {
            throw new IllegalArgumentException("文件路径不能为空");
        }

        System.out.println("开始处理JSON工时文件: " + filePath);

        try {
            // 1. 读取JSON文件
            List<ManhourRequest.ManhourItem> manhourItems = jsonFileService.readManhourDataFromFile(filePath);

            // 2. 提交工时数据
            int successCount = manhourApiService.submitBatchManhours(manhourItems);

            // 3. 返回结果
            SubmitResult result = new SubmitResult(manhourItems.size(), successCount);

            System.out.println("处理完成 - 总数: " + result.getTotalCount() +
                ", 成功: " + result.getSuccessCount() + ", 失败: " + result.getFailureCount());

            return result;

        } catch (Exception e) {
            System.err.println("处理JSON工时文件失败: " + filePath + ", 错误: " + e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("处理失败: " + e.getMessage(), e);
        }
    }

    /**
     * 提交结果统计
     */
    public static class SubmitResult {
        
        /**
         * 总记录数
         */
        private final int totalCount;
        
        /**
         * 成功数量
         */
        private final int successCount;
        
        /**
         * 失败数量
         */
        private final int failureCount;
        
        public SubmitResult(int totalCount, int successCount) {
            this.totalCount = totalCount;
            this.successCount = successCount;
            this.failureCount = totalCount - successCount;
        }
        
        public int getTotalCount() {
            return totalCount;
        }
        
        public int getSuccessCount() {
            return successCount;
        }
        
        public int getFailureCount() {
            return failureCount;
        }
        
        /**
         * 是否全部成功
         */
        public boolean isAllSuccess() {
            return failureCount == 0;
        }
        
        /**
         * 成功率
         */
        public double getSuccessRate() {
            if (totalCount == 0) {
                return 0.0;
            }
            return (double) successCount / totalCount * 100;
        }
        
        @Override
        public String toString() {
            return String.format("SubmitResult{总数=%d, 成功=%d, 失败=%d, 成功率=%.1f%%}", 
                totalCount, successCount, failureCount, getSuccessRate());
        }
    }
}
